//
//  QDSideslippingViewController.m
//  QuickDoctor
//
//  Created by ycw on 2016/12/20.
//  Copyright © 2016年 Eric. All rights reserved.
//

#import "QDSideslippingViewController.h"
#import "QDSideslippingCell.h"
#import "QDLoginController.h"
#import "QDSettingAndHelpController.h"
#import "QDShareController.h"
#import "QDAboutController.h"
#import "QDFocusController.h"
#import "QDCaseManagerController.h"
#import "QDApplyController.h"
#import "QDMainViewController.h"
#import "UMSocial.h"



#define AppKey @"5744171667e58e89d80015ae"
static NSString *cellID = @"sideslippingCell";

@interface QDSideslippingViewController ()<UITableViewDelegate,UITableViewDataSource,UMSocialUIDelegate>

@property(strong,nonatomic) NSArray<NSString *> *array;

@end

@implementation QDSideslippingViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self setUpUI];
    
}

#pragma mark
#pragma mark - 搭建界面
- (void) setUpUI
{
    
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    
    [self.view addSubview:tableView];
    
    //对tableView进行约束
    [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.edges.equalTo(self.view);
        
    }];
    
    tableView.delegate = self;
    
    tableView.dataSource = self;
    
    self.tableView = tableView;
    
    //让tableView不能滚动
    tableView.scrollEnabled = NO;
    
    BOOL isLogin = [[NSUserDefaults standardUserDefaults] boolForKey:KEYFORLOGIN];
    
    if (isLogin)
    {
        tableView.tableHeaderView = [self createHeaderView];
    }
    else
    {
        tableView.tableHeaderView = [self createLoginHeaderView];
    }
    
    
    [tableView registerClass:[QDSideslippingCell class] forCellReuseIdentifier:cellID];
    
    self.array = @[@"名医通申请",@"病例管理",@"关注医生",@"设置与帮助",@"关于产品",@"分享给好友"];
    
}

#pragma mark
#pragma mark - 实现数据源方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    
    return 2;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return 3;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    QDSideslippingCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID forIndexPath:indexPath];
    if (indexPath.section == 0) {
        cell.lable.text = self.array[indexPath.row];
    }else{
        cell.lable.text = self.array[indexPath.row + 3];
    }
    //cell.lable.text = self.array[indexPath.row];
    //cell.button.titleLabel = self.array[indexPath.row];
    
    //cell.imageView.image = [UIImage imageNamed:@"arrow_right_black"];
    
    //cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    return cell;
    
}

#pragma mark
#pragma mark - 设置表头
- (UIView *)createHeaderView
{
    
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 100)];
    
    headerView.backgroundColor = [UIColor colorWithRed:23/255.0 green:187/255.0 blue:187/255.0 alpha:1];
    
    UIImageView *imgV = [[UIImageView alloc] initWithFrame:CGRectMake(20, 20, 60, 60)];
    
    imgV.layer.masksToBounds = YES;
    
    imgV.layer.cornerRadius = 30;
    
    [headerView addSubview:imgV];
    
    imgV.image = [UIImage imageNamed:@"banner"];
    
    UILabel *lblName = [[UILabel alloc] init];
    
    lblName.text = @"王二才";
    
    lblName.font = [UIFont systemFontOfSize:18];
    
    [headerView addSubview:lblName];
    
    [lblName mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(imgV);
        
        make.leading.equalTo(imgV.mas_trailing).mas_offset(20);
        
        make.trailing.equalTo(headerView).mas_offset(-10);
        
    }];
    
    UILabel *lblNote = [[UILabel alloc] init];
    
    lblNote.text = @"查看个人资料";
    
    [headerView addSubview:lblNote];
    
    [lblNote mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.leading.equalTo(lblName);
        
        make.bottom.equalTo(imgV);
        
        //make.top.equalTo(lblName.mas_bottom).mas_offset(10);
        
        make.trailing.equalTo(headerView).mas_offset(-10);
        
    }];
    
    return headerView;
    
}

#pragma mark
#pragma mark - 设置登录的表头
- (UIView *)createLoginHeaderView
{
    
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 100)];
    
    headerView.backgroundColor = [UIColor colorWithRed:23/255.0 green:187/255.0 blue:187/255.0 alpha:1];
    
    UIImageView *imgV = [[UIImageView alloc] init];
    
    imgV.layer.masksToBounds = YES;
    
    imgV.layer.cornerRadius = 30;
    
    [headerView addSubview:imgV];
    
    [imgV mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.equalTo(headerView);
        
        make.centerX.equalTo(headerView).mas_offset(-30);
        
        make.height.width.mas_equalTo(60);
        
    }];
    
    imgV.image = [UIImage imageNamed:@"name"];
    
    UIButton *btnLogin = [[UIButton alloc] init];
    
    [btnLogin setTitle:@"登录" forState:UIControlStateNormal];
    //[btnLogin setBackgroundColor:[UIColor redColor]];
    
    //[btnLogin setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
    [headerView addSubview:btnLogin];
    
    [btnLogin mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.leading.equalTo(headerView).mas_offset(70);
        
        make.bottom.equalTo(headerView).mas_offset(-10);
        
        make.width.mas_equalTo(50);
        
        make.height.mas_equalTo(30);
        
    }];
    
    [btnLogin addTarget:self action:@selector(clickLoginButton) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *btnRegist = [[UIButton alloc] init];
    
    [btnRegist setTitle:@"注册" forState:UIControlStateNormal];
    
    [headerView addSubview:btnRegist];
    
    [btnRegist mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.trailing.equalTo(headerView).mas_offset(-130);
        
        make.bottom.equalTo(headerView).mas_offset(-10);
        
        make.width.mas_equalTo(50);
        
        make.height.mas_equalTo(30);
        
    }];
    
    
    
    return headerView;
    
}

#pragma mark
#pragma mark - 点击登录跳转的界面
- (void)clickLoginButton
{
    
    
    QDLoginController *loginVC = [[QDLoginController alloc] init];
    
    UINavigationController *naviC = [[UINavigationController alloc] initWithRootViewController:loginVC];
    
    [self presentViewController:naviC animated:NO completion:nil];
    
}

#pragma mark
#pragma mark - 封装一个方法,用来跳转
- (void)pushToController :(UIViewController *)naviController
{
    [self.sidePanelController showCenterPanelAnimated:YES];
    
    //QDMainViewController *VC = [[QDMainViewController alloc] init];
    
    UINavigationController *naviC = [[UINavigationController alloc] initWithRootViewController:naviController];
    
    naviC = (UINavigationController *)self.sidePanelController.centerPanel;
    
    [naviC pushViewController:naviController animated:YES];
    
}




- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    //取消选中的方法
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    //取出登录状态
    BOOL isLogin = [[NSUserDefaults standardUserDefaults] boolForKey:KEYFORLOGIN];
    
    if (!isLogin)
    {
        if (indexPath.section == 0)
        {
            return;
        }
    }
    QDApplyController *applyVC = [[QDApplyController alloc] init];
    
    QDCaseManagerController *caseVC = [[QDCaseManagerController alloc] init];

    QDFocusController *focusVC = [[QDFocusController alloc] init];
    
    QDSettingAndHelpController *settingVC = [[QDSettingAndHelpController alloc] init];
    QDAboutController *aboutVC = [[QDAboutController alloc] init];
    
    if (indexPath.section == 0) {
        switch (indexPath.row) {
            case 0:
                
                [self pushToController:applyVC];
                
                break;
            case 1:
                
                [self pushToController:caseVC];
                
                break;
            case 2:
                
                [self pushToController:focusVC];
                
                break;
                
            default:
                break;
        }
    }else{
    
        switch (indexPath.row) {
            case 0:
                
                [self pushToController:settingVC];
                
                break;
            case 1:
                
                [self pushToController:aboutVC];
                
                break;
            case 2:
                
                [UMSocialSnsService presentSnsIconSheetView:self
                                                     appKey:AppKey
                                                  shareText:@""
                                                 shareImage:[UIImage imageNamed:@"icon"]
                                            shareToSnsNames:@[UMShareToWechatSession,UMShareToQQ,UMShareToSina,UMShareToWechatTimeline,UMShareToQzone]
                                                   delegate:self];
            default:
                break;
    
        }
    }
    
    
}

///回调
-(void)didFinishGetUMSocialDataInViewController:(UMSocialResponseEntity *)response
{
    
    
    //根据`responseCode`得到发送结果,如果分享成功
    if(response.responseCode == UMSResponseCodeSuccess)
    {
        //得到分享到的平台名
        NSLog(@"share to sns name is %@",[[response.data allKeys] objectAtIndex:0]);
    }
}



@end
